package cn.gailvlun.gll.util;

import java.text.MessageFormat;



public class StringUtil {
    public static String format(String pattern, Object... arguments) {
        return MessageFormat.format(pattern, arguments);
    }

    static String CHN_NUMBER[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    static String CHN_UNIT[] = {"", "十", "百", "千"};          
    static String CHN_UNIT_SECTION[] = {"", "万", "亿", "万亿"}; 

    
    public static String num2Chn(int num) {
        StringBuffer returnStr = new StringBuffer();
        Boolean needZero = false;
        int pos=0;           
        if(num==0){
            
            returnStr.insert(0,CHN_NUMBER[0]);
        }
        while (num > 0) {
            int section = num % 10000;
            if (needZero) {
                returnStr.insert(0, CHN_NUMBER[0]);
            }
            String sectionToChn = sectionNumToChn(section);
            
            sectionToChn += (section != 0) ? CHN_UNIT_SECTION[pos] : CHN_UNIT_SECTION[0];
            returnStr.insert(0, sectionToChn);
            needZero = ((section < 1000 && section > 0) ? true : false); 
            pos++;
            num = num / 10000;
        }
        return returnStr.toString();
    }

    
    public static String sectionNumToChn(int section) {
        StringBuffer returnStr = new StringBuffer();
        int unitPos = 0;       

        Boolean zero = true;
        while (section > 0) {

            int v = (section % 10);
            if (v == 0) {
                if ((section == 0) || !zero) {
                    zero = true; 
                    
                    returnStr.insert(0, CHN_NUMBER[v]);
                }
            } else {
                zero = false; 
                StringBuffer tempStr = new StringBuffer(CHN_NUMBER[v]);
                tempStr.append(CHN_UNIT[unitPos]);  
                returnStr.insert(0, tempStr);
            }
            unitPos++; 
            section = section / 10;
        }
        return returnStr.toString();
    }

    public static String getFileNameByUrl(String url) {
        String[] splitArr = url.split("/");
        return splitArr[splitArr.length - 1];
    }

    public static boolean isAllSpace(String text) {
        String trimmedStr = text.replaceAll(" ", "");
        return IsEmpty.string(trimmedStr);
    }
}
